package com.cty._05_Ability._58_01_ReverseWordsInSentence;

/**
 * @Auther: cty
 * @Date: 2020/7/23 11:17
 * @Description: 面试题58（一）：翻转单词顺序
 * 题目：输入一个英文句子，翻转句子中单词的顺序，但单词内字符的顺序不变。
 * 为简单起见，标点符号和普通字母一样处理。例如输入字符串"I am a student. "，
 * 则输出"student. a am I"。
 * @version: 1.0
 */
public class ReverseWordsInSentence {

    public static String reverseWordsInSentence(String sentence){
        if(sentence == null)
            return null;
        if(sentence.equals(""))
            return "";

        String reverse = "";
        int index;
        while((index=sentence.lastIndexOf(' ')) != -1){
            reverse += sentence.substring(index).trim()+" ";
            sentence = sentence.substring(0,index);
        }
        reverse += sentence.trim();
        return reverse;
    }  // end reverseWordsInSentence()

}  // end class
